package org.dromara.third.convert.personAlarmData.web;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.PersonAlarmData;
import org.dromara.system.mapper.PersonAlarmDataMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.personAlarmData.dto.PersonAlarmDataDTO;
import org.dromara.third.convert.personAlarmData.validator.PersonAlarmDataValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertPersonAlarmDataController {

    @Autowired
    private PersonAlarmDataMapper personAlarmDataMapper;

    @PostMapping("/personAlarmData")
    @Transactional
    public R<Void> addPersonAlarmData(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        PersonAlarmDataDTO personAlarmDataDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            personAlarmDataDTO = JSONObject.parseObject(decrypt, PersonAlarmDataDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, personAlarmDataDTO, new PersonAlarmDataValidator());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        PersonAlarmData personAlarmData = assemblePersonAlarmData(orgCode, personAlarmDataDTO);
        int count = personAlarmDataMapper.countWithOutDelFlag(personAlarmData.getId());
        if (count == 0) {
            personAlarmDataMapper.insert(personAlarmData);
        } else {
            personAlarmDataMapper.updateWithOutDelFlag(personAlarmData);
        }
        return R.ok();
    }

    private PersonAlarmData assemblePersonAlarmData(String orgCode, PersonAlarmDataDTO dto) {
        PersonAlarmData personAlarmData = new PersonAlarmData();
        try {
            personAlarmData.setId(dto.getId());
            personAlarmData.setAlarmTime(DateUtils.parseDate(dto.getAlarmTime()));
            personAlarmData.setType(dto.getType());
            personAlarmData.setName(dto.getName());
            personAlarmData.setCardNo(dto.getCardNo());
            personAlarmData.setHandleTime(DateUtils.parseDate(dto.getHandleTime()));
            personAlarmData.setHandleStatus(dto.getHandleStatus());
            personAlarmData.setLongitude(dto.getLongitude());
            personAlarmData.setLatitude(dto.getLatitude());
            personAlarmData.setDelFlag(Long.parseLong(dto.getDelFlag()));
            personAlarmData.setCreateDept(Long.parseLong(dto.getCreateDept()));
            personAlarmData.setCreateBy(Long.parseLong(dto.getCreateBy()));
            personAlarmData.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            personAlarmData.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            personAlarmData.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            personAlarmData.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装人员报警信息异常");
        }
        return personAlarmData;
    }
}
